//
//  CommonUtil.m
//  Stock
//
//  Created by blacksun on 11-6-5.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CommonUtil.h"


@implementation CommonUtil




-(NSString *)pathOfResouceInMainBundle:(NSString *)fullFileName{
    if (fullFileName != nil) {
        NSArray *keywords = [fullFileName componentsSeparatedByString:@"."];
        NSString *suffix = [keywords objectAtIndex:[keywords count]-1];
        NSUInteger length = [fullFileName length] - [suffix length];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:[fullFileName substringToIndex:length-1] ofType:suffix];
        //NSLog(@"%@",path);
        return path;
    }
    return nil;
}


-(NSString *)pathOfResouceInDocumentDirectory:(NSString *)fullFileName{
    if (!fullFileName) {
        return nil;
    }
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths objectAtIndex:0];
    NSString *fullPath = [documentPath stringByAppendingPathComponent:fullFileName];
    return fullPath;
}
-(NSString *)pathOfResourceInTemporaryDirectory:(NSString *)fullFileName{
    NSString *temporaryPath = NSTemporaryDirectory();
    return [temporaryPath stringByAppendingPathComponent:fullFileName];
}

-(NSString *)pathOfStockImageFile:(NSString *)imageName{
    NSString *path;
    if ([StockContext shareInstance].isFavorite) {
        path = [self pathOfResouceInDocumentDirectory:imageName];
    }else{
        path = [self pathOfResourceInTemporaryDirectory:imageName];
    }
    //NSLog(@"%@",path);
    return path;
}


-(void)saveData:(NSData *)data inDocumentDirectoryWithFileName:(NSString *)fileName{
    NSString *fullPath = [self pathOfResouceInDocumentDirectory:fileName];
    [data writeToFile:fullPath atomically:YES];
}

-(void)copyMapEntries:(NSDictionary *)aMap toObject:(NSObject *)target{
    //NSObject *object = (NSObject *)target;
    //NSArray *attributeKeys = [object attributeKeys];
    for (NSString * key in [aMap allKeys]) {
        //if ([attributeKeys containsObject:key]) {
            [target setValue:[aMap valueForKey:key] forKey:key];
        //}
    }
}


-(UIImage *)addText:(UIImage *)img text:(NSString *)aText { 
    //get image width and height 
    int w = img.size.width; 
    int h = img.size.height; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    //create a graphic context with CGBitmapContextCreate 
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); 
    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage); 
    CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1); 
    char* text = (char *)[@"abcdss" cStringUsingEncoding:NSUTF8StringEncoding]; 
    CGContextSelectFont(context, "Georgia", 30,kCGEncodingFontSpecific); 
    CGContextSetTextDrawingMode(context, kCGTextFill); 
    CGContextSetRGBFillColor(context, 255, 0, 0, 1); 
    CGContextSetTextMatrix(context, CGAffineTransformMakeRotation( -M_PI/4 )); 
    CGContextShowTextAtPoint(context, 60, 390, text, strlen(text)); 
    //Create image ref from the context 
    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    UIImage *image = [UIImage imageWithCGImage:imageMasked];
    CGImageRelease(imageMasked);
    return image;
}

-(UIImage *)buildImageWithView:(UIView *)aView{
    UIGraphicsBeginImageContext(aView.frame.size);
    [aView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

//单例实现
static CommonUtil *util = nil;

+(id)shareInstance{
    @synchronized(self){
        if (util == nil) {
            //NSLog(@"alloc CommonUtil");
            util = [[self alloc] init];
        }
    }
    return util;
}
+ (id)allocWithZone:(NSZone *)zone{
    @synchronized(self) {
        if (util == nil) {
            util = [super allocWithZone:zone];
            return util;  // assignment and return on first allocation
        }
    }
    
    return nil; //on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone{
    return self;
}

- (id)retain{
    return self;
}

- (unsigned)retainCount{
    return UINT_MAX;  //denotes an object that cannot be released
}

- (void)release{
    //do nothing
}

- (id)autorelease{
    return self;
}

@end
